import { ref, reactive, onBeforeMount } from 'vue'

function defaultPage() {
  return {
    pageSizes: [10, 20, 30, 50, 100, 200, 500],
    pageNumber: 1,
    pageSize: 10,
    data: [],
    total: 0
  }
}

// 表格混合
export default function (initData: Function) {
  
  const page = reactive({
    pageSizes: [10, 20, 30, 50, 100, 200, 500],
    pageNumber: 1,
    pageSize: 10,
    data: [],
    total: 0
  })

  //搜索
  function search(pageNumber: number = 1, pageSize: number = 10) {
    page.pageNumber = pageNumber
    page.pageSize = pageSize
    initData(pageNumber, pageSize)
  }

  //选择
  const selectNum = ref(0)
  function changeSelect(selection: Array<any>) {
    selectNum.value = selection.length
  }

  //
  function initIndex(index: number) {
    return page.pageSize * (page.pageNumber - 1) + (index + 1)
  }

  function resetPage() {
    Object.assign(page, defaultPage())
  }

  onBeforeMount(() => {
    search()
  })

  return {
    page,
    selectNum,
    search,
    changeSelect,
    initIndex
  }
}
